<?php

namespace app\hotel\controller;

use think\Controller;

class Collect extends Controller
{
    public function __construct(){
        parent::__construct();

        $this->CollectModel = model('Hotel.Collect');

        $this->BusinessModel = model('Business.User');

        $this->RoomModel = model('Hotel.room');
    }


    // 收藏房间
    public function add(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $roomid = $this->request->param('roomid',0,'trim');

            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询房间
            $room = $this->RoomModel->find($roomid);
            if(!$roomid){
                $this->error('房间未知');
                exit;
            }

            $data = [
                'busid' => $busid,
                'roomid' => $roomid
            ];

            // 查询是否有收藏过
            $collect = $this->CollectModel->where($data)->find();
            if(!$collect){
                $result = $this->CollectModel->validate('common/Hotel/Collect')->save($data);

                if($result === FALSE){
                    $this->error('收藏失败');
                    exit;
                }else{
                    $this->success('收藏成功');
                    exit;
                }
            }else{
                $result = $this->CollectModel->where(['id' => $collect['id']])->delete();
                if($result === FALSE){
                    $this->error('取消收藏失败');
                    exit;
                }else{
                    $this->success('取消收藏成功');
                    exit;
                }
            }

            
        }
    }


    // 收藏列表
    public function index(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $page = $this->request->param('page',1,'trim');
            $limit = 10;
            $offset = ($page - 1) * $limit;
            // 查询用户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询收藏列表
            $collect = $this->CollectModel->with(['room'])->where(['busid' => $busid])->limit($offset,$limit)->select();

            if(!$collect){
                $this->error('暂无更多数据');
                exit;
            }else{
                $this->success('返回数据',null,$collect);
                exit;
            }
        }
    }

}
